using System;
using System.Collections;
using RemoteObject;
using System.Collections.Generic;
using MessageCommon.GlobalStructure;

namespace RemoteBase
{
    /// <remarks>
    /// Sample object to demonstrate the use of .NET Remoting.
    /// </remarks>
    public class SampleObject : MarshalByRefObject
    {
        public Dictionary<string, AddressInfoList> m_dicUserIPList = new Dictionary<string, AddressInfoList>();

        Hashtable hTChatMsg = new Hashtable();
        ArrayList alOnlineUser = new ArrayList();
        private int key = 0;

        public void RegisterUser(string strName, AddressInfo address)
        {
            alOnlineUser.Add(strName);
            if (!m_dicUserIPList.ContainsKey(strName))
            {
                m_dicUserIPList.Add(strName, AddAddress(null, address));
            }
            else
            {
                m_dicUserIPList[strName] = AddAddress(m_dicUserIPList[strName], address);
            }
        }

        public void RegisterUser()
        {

        }

        public void UnRegisterUser(string strName, AddressInfo address)
        {
            if (m_dicUserIPList.ContainsKey(strName))
            {
                AddressInfoList lstAdress = m_dicUserIPList[strName];
                if (lstAdress != null)
                {
                    lstAdress.RemoveAll(addr =>
                    {
                        return address == addr;
                    });
                }
            }
        }

        public AddressInfoList GetUserOnlineAddress(string strName)
        {
            return m_dicUserIPList[strName];
        }

        private AddressInfoList AddAddress(AddressInfoList lstAddress, AddressInfo address)
        {
            if (lstAddress == null)
            {
                lstAddress = new AddressInfoList();
            }

            lstAddress.Add(address);
            return lstAddress;
        }

        public bool JoinToChatRoom(string name)
        {
            if (alOnlineUser.IndexOf(name) > -1)
                return false;
            else
            {
                alOnlineUser.Add(name);
                MessageInfo info = new MessageInfo();
                info.strRecevier = "everyone";
                info.strBody = name + " has joined into chat room.";
                SendMsgToSvr(info);
                return true;
            }

        }
        public void LeaveChatRoom(string name)
        {
            alOnlineUser.Remove(name);
            MessageInfo info = new MessageInfo();
            info.strRecevier = "everyone";
            info.strBody = name + " has left the chat room.";
            SendMsgToSvr(info);
        }
        public ArrayList GetOnlineUser()
        {
            return alOnlineUser;
        }

        public int CurrentKeyNo()
        {
            return key;
        }
        public void SendMsgToSvr(MessageInfo chatMsgFromUsr)
        {
            //chatMsg = chatMsgFromUsr;
            hTChatMsg.Add(++key, chatMsgFromUsr);
        }
        public MessageInfo GetMsgFromSvr(int lastKey)
        {
            if (key > lastKey)
                return hTChatMsg[lastKey + 1] as MessageInfo;
            else
                return null;
        }
    }
}
